/**
 * Description: RegisterInfo.java
 * Copyright (c) 2008-2009  Neo. 
 * All Rights Reserved.
 * @version 1.0  Mar 30, 2009 9:33:47 PM 李萌（neolimeng@gmail.com）created
 */
package org.paradise.dms.dao.impl;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.paradise.dms.pojo.RegisterInfo;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Service;

import com.dheaven.framework.dao.BaseHibernateDao;
import com.dheaven.framework.dao.DaoException;

/**
 * Description: Copyright (c) 2008-2009 Neo. All Rights Reserved.
 * 
 * @version 1.0 Mar 30, 2009 9:33:47 PM 李萌（neolimeng@gmail.com）created
 */
@Service
public class RegisterInfoDAOImpl extends BaseHibernateDao<RegisterInfo> {
	/**
	 * 
	 * Description: DAO-插入一条物品等级记录
	 * 
	 * @Version1.0 Mar 30, 2009 9:44:37 PM 李萌（neolimeng@gmail.com）创建
	 * @param registerinfo
	 * @throws DaoException
	 */
	public void insertRegisterInfo(RegisterInfo registerinfo)
			throws DaoException {
		this.create(registerinfo);
	}

	/**
	 * 
	 * 
	 * Description: DAO-更新一条物品等级记录
	 * 
	 * @Version1.0 Mar 30, 2009 9:45:14 PM 李萌（neolimeng@gmail.com）创建
	 * 
	 * @param registerinfo
	 * @throws DaoException
	 */
	public void updateRegisterInfo(RegisterInfo registerinfo)
			throws DaoException {
		this.update(registerinfo);
	}

	/**
	 * 
	 * Description: DAO-删除一条物品等级记录--真删除
	 * 
	 * @Version1.0 Mar 30, 2009 9:44:37 PM 李萌（neolimeng@gmail.com）创建
	 * @param registerinfo
	 * @throws DaoException
	 */
	// public void deleteRegisterInfo(int registerinfoid) throws DaoException{
	// RegisterInfo ri=new RegisterInfo();
	// ri.setRegisterinfoid(registerinfoid);
	// this.delete(ri);
	// }
	/**
	 * 
	 * Description: DAO-删除一条物品等级记录--假删除
	 * 
	 * @Version1.0 Mar 30, 2009 9:44:37 PM 李萌（neolimeng@gmail.com）创建
	 * @param registerinfo
	 * @throws DaoException
	 */
	public void deleteRegisterInfo(int registerinfoid) {
		SQLQuery sqlQuery = (SQLQuery) this
				.getSession()
				.createSQLQuery(
						"update registerinfo set registerinfoind=0 where registerinfoid=?")
				.setInteger(0, registerinfoid);
		sqlQuery.executeUpdate();
	}

	/**
	 * 
	 * Description: DAO-取得一条物品等级记录
	 * 
	 * @Version1.0 Mar 30, 2009 9:44:37 PM 李萌（neolimeng@gmail.com）创建
	 * @param registerinfo
	 * @throws DaoException
	 */
	public RegisterInfo getRegisterInfo(int registerinfoid) throws DaoException {
		return (RegisterInfo) this
				.find(
						"from RegisterInfo where registerinfoid=? and registerinfoind=1",
						new Object[] { registerinfoid }).get(0);
	}

	/**
	 * 
	 * Description: DAO-取得所有物品登记记录
	 * 
	 * @Version1.0 Mar 30, 2009 9:44:37 PM 李萌（neolimeng@gmail.com）创建
	 * @param registerinfo
	 * @throws DaoException
	 */
	@SuppressWarnings("unchecked")
	public List<RegisterInfo> getAllRegisterInfo() {
		List<RegisterInfo> lri = this
				.find("from RegisterInfo where registerinfoind = 1");
		return lri;
	}

	@SuppressWarnings("unchecked")
	public int getRows() {
		List<RegisterInfo> lri = this
				.find("from RegisterInfo where registerinfoind=1");
		return lri.size();
	}

	@SuppressWarnings("unchecked")
	public List<RegisterInfo> listAllRegisterInfo(int pageSize, int startRow) {
		final int pageSize1 = pageSize;
		final int startRow1 = startRow;
		return this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public List doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session
						.createQuery("from RegisterInfo where registerinfoind=1");
				query.setFirstResult(startRow1);
				query.setMaxResults(pageSize1);
				return query.list();
			}
		});
	}

	public void setReturn(int registerinfoid) {
		SQLQuery sqlQuery = (SQLQuery) this
				.getSession()
				.createSQLQuery(
						"update registerinfo set registereturntime=? where registerinfoid=?")
				.setTimestamp(0, new Date()).setInteger(1, registerinfoid);
		sqlQuery.executeUpdate();
	}
}
